(library (port-utils)
  (export
   port-u8-for-each)
  (import
    (except (rnrs base) let-values)
    (only (guile)
          lambda* λ
          error
          eof-object?
          simple-format
          current-output-port
          current-input-port
          call-with-output-string
          call-with-input-string
          call-with-output-file
          remainder
          array-for-each)
    (prefix (logging) log:)
    (ice-9 binary-ports)
    ;; Bytevectors are created by procedures from (ice-9 binary-ports), but to
    ;; work with them, other imports are needed.
    (rnrs bytevectors)
    (ice-9 textual-ports)))


(define port-u8-for-each
  (λ (proc port)
    "proc is called as follows: (proc index-of-u8 u8)"

    (let iter ([results '()] [index 0] [u8 (get-u8 port)])
      (cond
       [(eof-object? u8) (reverse results)]
       [else
        (iter (cons (proc index u8) results)
              (+ index 1)
              (get-u8 port))]))))
